package edu.uic.kott.fragments;

import java.util.List;

import edu.uic.keyoutcomestrackertool.R;
import edu.uic.kott.activities.TrackKeyOutcomes;
import edu.uic.kott.db.KottDataSource;
import edu.uic.kott.model.ClassActivity;
import android.os.Bundle;
import android.app.Fragment;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TableLayout;
import android.widget.TextView;

public class TrackActivities extends Fragment {
	
	static TextView classActivitiesOn;
	static String activitiesDate;
	
	private TableLayout activitiesQueryLayout;
	
	KottDataSource datasource;
	List<ClassActivity> activityList;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View track_activities_view = inflater.inflate(R.layout.track_activities, container, false);
		
		classActivitiesOn = (TextView) track_activities_view.findViewById(R.id.classActivitiesOn);
		classActivitiesOn.setText(TrackKeyOutcomes.activeDate);
		
		activitiesQueryLayout = (TableLayout) track_activities_view.findViewById(R.id.activitiesQueryLayout);
		
		TrackKeyOutcomes.activitiesVisible = true;
		TrackKeyOutcomes.UpdateVariables();
		
		ActivitiesData();
		
		return track_activities_view;
	}

	private void ActivitiesData() {
		datasource = new KottDataSource(this.getActivity());
		
		//query =
		
		//Activity activity = new Activity();
		
		//MAKE GUI
		/*for (Activity a : query) {
			
			
		}*/
		
		//Above should end up looping like this
		makeActivitiesGUI(0, "IDS406", "final", "KOTT app", "5/1/14", "Key Outcomes Tracker Tool Application", 0);
		makeActivitiesGUI(1, "IDS495", "presentation", "Capsim", "4/29/14", "Capsim Presentation", 1);
	}
	
	public void makeActivitiesGUI(long classId, String className, String activityType, String activityName, String dueDate, 
			String description, int index) {
		LayoutInflater inflater = (LayoutInflater) this.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		View newRowView = inflater.inflate(R.layout.activities_group_view, null);
		
		TextView classText = (TextView) newRowView.findViewById(R.id.activitiesClassName);
		classText.setText(className);
		
		TextView typeText = (TextView) newRowView.findViewById(R.id.activitiesType);
		typeText.setText(activityType);
		
		TextView nameText = (TextView) newRowView.findViewById(R.id.activitiesName);
		nameText.setText(activityName);
		
		TextView dueText = (TextView) newRowView.findViewById(R.id.activitiesDueDate);
		dueText.setText(dueDate);
		
		TextView descText = (TextView) newRowView.findViewById(R.id.activitiesDescription);
		descText.setText(description);
		
		CheckBox completedCheckBox = (CheckBox) newRowView.findViewById(R.id.activitiesCompletedCheckBox);
		completedCheckBox.setOnCheckedChangeListener(completedListener);
		
		activitiesQueryLayout.addView(newRowView, index);
	}
	
	public OnCheckedChangeListener completedListener = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			//Completed
			
		}
		
	};

	public static void ActivitiesUpdateOnDateChange() {
		activitiesDate = TrackKeyOutcomes.activeDate;
		classActivitiesOn.setText("on: " + activitiesDate);
	}
	
	@Override
	public void onStop() {
		TrackKeyOutcomes.activitiesVisible = false;
		super.onStop();
	}
	
	@Override
	public void onResume() {
		TrackKeyOutcomes.activitiesVisible = true;
		super.onResume();
	}
}